<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 3forcom Element Helper
 *
 * @package		3forcom CMS
 * @subpackage	Helpers
 * @category	Helpers
 * @author		Nghia Ho
 */

// ------------------------------------------------------------------------

/**
 * Read File
 *
 * Opens the file specfied in the path and returns it as a string.
 *
 * @access	public
 * @param	string	path to file
 * @return	string
 */
if ( ! function_exists('textfield'))
{
	function textfield($name,$label=null,$value=null,$type = 'text')
	{
		$label = (empty($label))?ucwords(str_replace('_', ' ', $name)):$label;
		return "<tr class=\"elements\">
        		<th scope=\"row\" >$label</th>
          		<td align=\"left\">
          			<input type='$type' name=\"$name\" value=\"$value\" />
          		</td>
        	</tr>";
	}
	function plaintext($name,$label=null,$value=null)
	{
		$label = (empty($label))?ucwords(str_replace('_', ' ', $name)):$label;
		return "<tr class=\"elements\">
        		<th scope=\"row\" >$label</th>
          		<td align=\"left\">
          			<textarea cols=\"70\" rows=\"5\" name=\"$name\" >$value</textarea>
          		</td>
        	</tr>";
	}
	function richtext($name,$label=null,$value=null)
	{
		$label = (empty($label))?ucwords(str_replace('_', ' ', $name)):$label;
		return "<tr class=\"elements\">
        		<th scope=\"row\" >$label</th>
          		<td align=\"left\">
          			<textarea id=\"$name\" name=\"$name\" >$value</textarea>
          			<script type=\"text/javascript\">
          				initEditor('$name')							
					</script>	
          		</td>
        	</tr>
        	";
	}
	function datefield($name,$label=null,$value=null)
	{
		$label = (empty($label))?ucwords(str_replace('_', ' ', $name)):$label;
		return "<tr class=\"elements\"> 
            	<th scope=\"row\" >$label</th>
            	<td align=\"left\"><input name=\"$name\" type=\"text\" value=\"$value\" class=\"dateField\"/></td>
          	</tr>
        	";
	}
	function gallery($name,$label=null,$value=null)
	{
		$label = (empty($label))?ucwords(str_replace('_', ' ', $name)):$label;
		$dataArr = array('id'=>null,'folder_name'=>null);		
		if(!empty($value)){
			$ci = get_instance();
			$ci->load->model('Folder');
        	$dataArr = $ci->Folder->get_by_field(array('id'=>$value),'*',true);        
		}
		$id_callback = str_replace(']', '', str_replace('[', '_', $name));
		return "<tr class=\"elements\">
	        	<th scope=\"row\" >$label</th>
	          	<td align=\"left\">
	          		<input readonly=\"readonly\" id=\"{$id_callback}_text\" value=\"{$dataArr['folder_name']}\" type=\"text\" />
	          		<input type=\"button\" value=\"Select folder\" onclick=\"thickBoxGallery('$id_callback');\" />
	          		<input type=\"hidden\" id=\"$id_callback\" name=\"$name\" value=\"{$dataArr['id']}\" />
	          	</td>
        	</tr>
        	";
	}
	function dropdown($name , $label=null, $options = array(), $selected = array(), $extra = '')
	{
		$label = (empty($label))?ucwords(str_replace('_', ' ', $name)):$label;
		return "<tr class=\"elements\">
	        	<th scope=\"row\" >$label</th>
	          	<td align=\"left\">".form_dropdown($name, $options, $selected, $extra)."</td>
        	</tr>
        	";
	}
	function image($name,$label=null,$value=null)
	{
		$label = (empty($label))?ucwords(str_replace('_', ' ', $name)):$label;
		return "<tr class=\"elements\"> 
            <th scope=\"row\" >Picture</th> 
            <td><table> 
              <tr> 
                <td><input type=\"file\" name=\"$name\" /></td>".
                ((empty($value))?null:("<td><img src=\"".base_url().'public/upload/fix/'.$value."\" width=\"165\" height=\"124\" /></td>")).
              "</tr>
            </table></td> 
          </tr>
        	";
	}
	function front_gallery($folder_id,$language=null,$start=null,$limit=null){
		if(empty($folder_id)) return array();
		$ci = get_instance();
		$ci->load->model('Picture');
              $dataArr = $ci->Picture->get_list($folder_id,$start,$limit,'folder_picture.order','ASC',((!empty($language))?array('language'=>$language):null),'*');
           return $dataArr;
	}
	function front_element($type,$name,$elements,$language=null,$start=null,$limit=null){
		$result = null;
		if(isset($elements[$type][$name])){
			switch($type){
				case 'richtext':
								$result = html_entity_decode($elements[$type][$name]);
								break;
				case 'gallery':
								$result = front_gallery(front_gallery($elements[$type][$name]),$language,$start,$limit);
								break;
				default:
								$result = $elements[$type][$name];											
			}
		}
		return $result;
	}
    function model_dropdown($model,$field_value,$field_text,$label=null,$selected=null)
	{
            //agent_id
            $ci = get_instance();
            $ci->load->model($model);
            $dataArr = $ci->$model->get_all();
            $options = array();
            $options['0'] =  '[ Choose ]';
            if (!is_array($field_text)){
                foreach ($dataArr as $item){
                   $options[$item[$field_value]] =  $item[$field_text];
                }
            }else{
                foreach ($dataArr as $item){
                   $options[$item[$field_value]] =  $item[$field_text[0]]." - ".$item[$field_text[1]];
                }
            }
                $name = strtolower($model);
                $name = str_replace("_model", "", $name);
		return "<tr>
	        	<th scope=\"row\" >$label</th>
	          	<td align=\"left\">".form_dropdown($name.'_id', $options, $selected)."</td>
        	</tr>
        	";
	}
       
       
}